package org.jflame.devAide.util;

import org.jflame.devAide.AppContext;
import org.jflame.fx.control.JFLAlert;
import org.jflame.fx.control.JFLDecorator;
import org.jflame.fx.control.SVGGlyph;
import org.jflame.fx.converter.NumberFieldFormatter;

import org.controlsfx.dialog.ExceptionDialog;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.Glyph;
import org.controlsfx.glyphfont.GlyphFont;
import org.controlsfx.glyphfont.GlyphFontRegistry;

import javafx.scene.Parent;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * 提供一些控件的便捷方法
 *
 * @author yucan.zhang
 */
public final class UIUtils {

    public static JFLDecorator decorator(Stage stage, Parent parent, String title, String icon) {
        // JFXDecorator decorator = new JFXDecorator(stage, parent, false, true, true);
        JFLDecorator decorator = new JFLDecorator(stage, parent, false, true, true);
        decorator.setCustomMaximize(true);
        decorator.setTitle(title);
        if (icon != null) {
            decorator.setGraphic(UIUtils.createImageView(ResourceUtils.imageUrl(icon), 16, 16));
        }
        return decorator;
    }

    /**
     * 从FONT_AWESOME获取一个图标
     * 
     * @param glyphName
     * @return
     */
    public static Glyph createFontGlyph(String glyphName, double size) {
        return FontAwesome().create(glyphName)
                .size(size);
    }

    public static Glyph createFontGlyph(FontAwesome.Glyph glyph, double size) {
        return FontAwesome().create(glyph)
                .size(size);
    }

    public static GlyphFont FontAwesome() {
        return GlyphFontRegistry.font("FontAwesome");
    }

    /**
     * 用给定的图片路径创建一个ImageView,并指定图片宽高
     *
     * @param imagePath 图片路径
     * @param width 图片宽度
     * @param height 图片高度
     * @return
     */
    public static ImageView createImageView(String imagePath, double width, double height) {
        ImageView image = new ImageView(imagePath);
        image.setFitWidth(width);
        image.setFitHeight(height);
        return image;
    }

    public static void showException(String errorMsg, Exception e) {
        showException(errorMsg, null, e);
    }

    public static void showException(String errorMsg, String logMsg, Exception e) {
        ExceptionDialog dlg = new ExceptionDialog(e);
        String msg = errorMsg == null ? "程序异常" : locale(errorMsg);
        dlg.setHeaderText(msg);
        dlg.getDialogPane()
                .getStylesheets()
                .addAll(AppContext.BASE_STYLE_FILES);
        if (logMsg == null) {
            logMsg = msg;
        }
        Loger.error(logMsg, e);
        dlg.showAndWait();
    }

    public static void info(String message) {
        JFLAlert.info(locale(message), AppContext.mainStage());
    }

    public static void warn(String error) {
        JFLAlert.error(locale(error), AppContext.mainStage());
    }

    public static SVGGlyph createSvgNode(String name, String colorWeb, Double size) {
        String svgContent = ResourceUtils.getSvgContent(name);
        if (svgContent != null) {
            SVGGlyph svgGlyph = colorWeb != null ? new SVGGlyph(svgContent, Color.web(colorWeb))
                    : new SVGGlyph(svgContent);
            if (size != null) {
                svgGlyph.setSize(size);
            }
            return svgGlyph;
        }
        return null;
    }

    public static SVGGlyph createSvgNode(String name, double size) {
        return createSvgNode(name, null, size);
    }

    public static SVGGlyph createSvgNode(String name, String colorWeb) {
        return createSvgNode(name, colorWeb, null);
    }

    public static SVGGlyph createSvgNode(String name) {
        return createSvgNode(name, null, null);
    }

    /*public static Alert createAlert(String errorMsg) {
        Alert alert = new Alert(AlertType.INFORMATION, errorMsg, ButtonType.OK);
    
        alert.getDialogPane()
                .setStyle("-fx-background-color:#fff;-fx-border-width:1px;-fx-border-color:#ccc");
        alert.setHeaderText(null);
        alert.initStyle(StageStyle.UNDECORATED);
    
        alert.getDialogPane()
                .getContent()
                .setOnMousePressed(new EventHandler<MouseEvent>() {
    
                    @Override
                    public void handle(MouseEvent event) {
                        double x,y;
                        x = event.getSceneX();
                        y = event.getSceneY();
                    }
    
                });
    
        Glyph icon = GlyphFontRegistry.font("FontAwesome")
                .create(FontAwesome.Glyph.EXCLAMATION_CIRCLE)
                .color(Color.WHITE)
                .size(18);
        alert.setGraphic(icon);
        return alert;
    }*/

    /**
     * 设置spinner为只可输入整数.初始值为最小值
     * 
     * @param spinner
     * @param min
     * @param max
     */
    public static void setSpinnerForInteger(Spinner<Integer> spinner, int min, int max) {
        setSpinnerForInteger(spinner, min, max, min);
    }

    /**
     * 设置spinner为只可输入整数.
     * 
     * @param spinner
     * @param min
     * @param max
     * @param initialValue 初始值
     */
    public static void setSpinnerForInteger(Spinner<Integer> spinner, int min, int max, int initialValue) {
        setSpinnerForInteger(spinner, min, max, initialValue, 1);
    }

    /**
     * 设置spinner为只可输入整数
     * 
     * @param spinner
     * @param min
     * @param max
     * @param initialValue 初始值
     * @param amountToStepBy 增长步长
     */
    public static void setSpinnerForInteger(Spinner<Integer> spinner, int min, int max, int initialValue,
            int amountToStepBy) {
        /*spinner.setValueFactory(new JFLIntegerSpinnerValueFactory(min, max, initialValue, amountToStepBy));*/
        SpinnerValueFactory.IntegerSpinnerValueFactory valueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(
                min, max, initialValue, amountToStepBy);
        // valueFactory.setConverter(new SafeIntegerStringConverter());
        spinner.setValueFactory(valueFactory);
        spinner.getEditor()
                .setTextFormatter(new NumberFieldFormatter());
    }

    /**
     * 给spinner设置值
     * 
     * @param <T>
     * @param spinner
     * @param value
     */
    public static <T> void setValue(Spinner<T> spinner, T value) {
        spinner.getValueFactory()
                .setValue(value);
    }

    private static String locale(String message) {
        // System.out.println(ResourceUtils.getString(message.substring(1)));
        if (message == null) {
            return message;
        }
        return message.charAt(0) == '%' ? ResourceUtils.getString(message.substring(1)) : message;
    }
}
